package by.blog.web.commands;

import by.blog.dao.beans.Film;
import by.blog.web.Converter;
import by.blog.service.FilmService;
import by.blog.service.beans.UIFilm;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 *
 */
public class EditCommand implements Command {

    @Override
    public void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html; charset=UTF-8");
        request.setCharacterEncoding("UTF-8");

        FilmService filmService = new FilmService();
        Converter converter = new Converter();
        UIFilm uiFilm;

        Film film;
        int id=0;

        try {
            id = Integer.parseInt(request.getParameter("id"));
            film = filmService.readUnit(id);
            uiFilm = converter.convertFilmToUIFilm(film);
            request.setAttribute("film", uiFilm);
        }
        catch (NumberFormatException e) {

        }

        RequestDispatcher dispatcher = request.getRequestDispatcher("/jsp/single.jsp");
        dispatcher.forward(request, response);
    }

}


